#!/usr/bin/python
# -*- coding: utf-8 -*-
import re,sys,optparse,urllib2
user='nanodesu@in-mail.ru'
passwd='splenchb'
def parseopt(arg):
    usage = "usage: %prog [options] url album."
    parser = optparse.OptionParser(usage=usage)
    parser.add_option('-n','--number-pages',action='store', default=None, dest='pages', help='Number page')
    parser.add_option("-a", action="store_true", dest="auth", help='Enable auth on id.cod.ru')
    #parser.add_option('-u','--auth',action='store', default=0, dest='auth', help='Auth')
    #parser.add_option('-d', '--diff', action="store_true", dest="diff", help='Diff')
    
    opt, arguments = parser.parse_args(args=arg,)
    try:
        name = arguments[0]
    except IndexError:
        parser.print_help()
        print "No parametrs. "
        sys.exit()
    return opt, arguments
def parse_optpages(optpages,pages):
        op=optpages
        op=op.split("-")
        #print len(op), op, pages
        if optpages == None:
            if pages == []:
                n=range(1,2)
            else:
                n=int(pages[0])
                n=range(1,n+1)
        elif len(op) == 1:
            n=int(optpages)
            n=range(n,n+1)
        else:
            n=range(int(op[0]),int(op[1])+1)
            
        return n
    
def auth_id_cod(user,passwd):
    cookes= urllib2.urlopen('https://id.cod.ru/auth?email=%s&password=%s' %(user,passwd))
    cookes=str(cookes.info())
    #print cookes
    cookes=re.findall('(codsid=.+?;)[\s\S\w\W]*(auth=YES;)[\s\S\w\W]*(modified=\d+?;)', cookes)[0]
    cookes='%s %s %s' %(cookes[0],cookes[1],cookes[2])
    #print cookes
    
    return cookes
def main(arg):
    opt, arguments=parseopt(arg)
    #print opt
    url=arguments[0]
    urls=[]
    src=urllib2.urlopen(url)
    pages=re.findall('<td width="80%" align="center" class="f12">.*<b>(\d{1,})</b></td>',\
                     src.read())
    #print pages
        
    
    if opt.auth == True:
        auth = auth_id_cod(user, passwd) 
    else:
        auth = ''
        
    
    n=parse_optpages(opt.pages,pages)
    
    src=get_pages(url, n, auth)
    print parse_pages(src)
 
def parse_pages(src):
    urls=re.findall('(http://avangard.photo.cod.ru/photos.*/w100_.*)" alt=',src)
    a=len(urls)
    bb_urls=''
    for i in range(a):
        urlre=re.findall('(http://.*)w100_(.*)',urls[i])[0]
        url='%s%s' %(urlre[0],urlre[1])
        tmb='%sw400_%s' %(urlre[0],urlre[1])
        bb_urls= '%s[URL=%s][IMG]%s[/IMG][/URL]\n' %(bb_urls,url,tmb)
    return bb_urls

def get_pages(url,n,auth):
    src=''
    for i in n:
        gurl='%s?page=%s' %(url,i)
        
        #print gurl
        
        req = urllib2.Request(gurl)
        req.add_header('Cookie', auth)
        url_data=urllib2.urlopen(req)
        
        src='%s%s' %(src,url_data.read())
    return src

main(sys.argv[1:])


#opt, arguments=parseopt(sys.argv[1:])
#set_pages=opt.pages
#set_pages=set_pages.split(' ')

#print opt.pages, arguments



#url='http://avangard.photo.cod.ru/users/Apkawa/6898581509/'
#data = auth_id_cod(user, passwd)
#req = urllib2.Request(url)
#req.add_header('Cookie', data)
#url_data=urllib2.urlopen(req).read()
#
#print url_data


#https://id.cod.ru/auth/?email=apkawa@gmail.com&password=bectusto&retURL=http://avangard.photo.cod.ru/users/Apkawa/2950692864/